package com.wanbatv.wanba.ahitv.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.wanbatv.wanba.ahitv.Fittimeutil.FittimeFengxiangDemo;
import com.wanbatv.wanba.ahitv.R;
import com.wanbatv.wanba.ahitv.util.Util;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;


public class FittimeFengxiangActivity extends Activity {

    private TextView fit_main_fengxiang_time;
    private static final int msgKey1 = 1;
    private ImageView fit_fengxiang_all;
    private ImageView fit_fengxiang_geren;
    private RelativeLayout fit_fengxiang_bg;
    private ViewPager viewpager;
    private ViewPager viewpagerall;

    private FittimeErjiFengxiangAdapter mAdapter = null;
    private FittimeErjiFengxiangAdapterall mAdapterall = null;
    ArrayList<View> viewContainter = new ArrayList<View>();
    ArrayList<View> viewContainterall = new ArrayList<View>();
    private ImageView fittime_fengxian_jiantou1;
    private ImageView fittime_fengxian_jiantou2;
    private TextView fittime_fengxiang_yema;
    private int YEMA_VIEW=0;
    private int YEMA_VIEWALL=0;
    private Map<String,Bitmap> map=new HashMap<String,Bitmap>();
    int YEMA_NUM=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fittime_fengxiang);
        fit_main_fengxiang_time=(TextView)findViewById(R.id.fit_main_fengxiang_time);
        new TimeThread().start();
        fit_fengxiang_all=(ImageView)findViewById(R.id.fit_fengxiang_all);
        fit_fengxiang_geren=(ImageView)findViewById(R.id.fit_fengxiang_geren);
        fit_fengxiang_bg=(RelativeLayout)findViewById(R.id.fit_fengxiang_bg);

        fittime_fengxian_jiantou1=(ImageView)findViewById(R.id.fittime_fengxian_jiantou1);
        fittime_fengxian_jiantou2=(ImageView)findViewById(R.id.fittime_fengxian_jiantou2);

        fittime_fengxiang_yema=(TextView)findViewById(R.id.fittime_fengxiang_yema);

        fit_fengxiang_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fit_fengxiang_bg.setBackgroundResource(R.drawable.fit_fengxiang_all_bg);
                viewpagerall.setVisibility(View.VISIBLE);
                viewpager.setVisibility(View.GONE);
                fittime_fengxiang_yema.setText(viewpagerall.getCurrentItem() + 1 + "/" + viewContainterall.size());
               if(viewContainterall.size()>1) {
                   if (viewpagerall.getCurrentItem() > 0) {
                       fittime_fengxian_jiantou1.setVisibility(View.VISIBLE);
                       fittime_fengxian_jiantou2.setVisibility(View.VISIBLE);
                   }
                   if (viewpagerall.getCurrentItem() == viewContainterall.size() - 1) {

                           fittime_fengxian_jiantou1.setVisibility(View.VISIBLE);
                           fittime_fengxian_jiantou2.setVisibility(View.GONE);

                   }
                   if(viewpagerall.getCurrentItem() ==0){
                       fittime_fengxian_jiantou1.setVisibility(View.GONE);
                       fittime_fengxian_jiantou2.setVisibility(View.VISIBLE);
                   }
               }else{
                   fittime_fengxian_jiantou1.setVisibility(View.GONE);
                   fittime_fengxian_jiantou2.setVisibility(View.GONE);
               }
            }
        });
        fit_fengxiang_geren.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fit_fengxiang_bg.setBackgroundResource(R.drawable.fit_fengxiang_geren_bg);
                viewpagerall.setVisibility(View.GONE);
                viewpager.setVisibility(View.VISIBLE);
                fittime_fengxiang_yema.setText(viewpager.getCurrentItem() + 1 +"/"+viewContainter.size());
                if(viewContainter.size()>1) {
                    if (viewpager.getCurrentItem() > 0) {
                        fittime_fengxian_jiantou1.setVisibility(View.VISIBLE);
                        fittime_fengxian_jiantou2.setVisibility(View.VISIBLE);
                    }
                    if (viewpager.getCurrentItem() == viewContainter.size() - 1) {

                            fittime_fengxian_jiantou1.setVisibility(View.VISIBLE);
                            fittime_fengxian_jiantou2.setVisibility(View.GONE);
                    }
                    if(viewpager.getCurrentItem() ==0){
                        fittime_fengxian_jiantou1.setVisibility(View.GONE);
                        fittime_fengxian_jiantou2.setVisibility(View.VISIBLE);
                    }
                }else{
                    fittime_fengxian_jiantou1.setVisibility(View.GONE);
                    fittime_fengxian_jiantou2.setVisibility(View.GONE);
                }
            }
        });
        viewpager=(ViewPager)findViewById(R.id.fit_fengxiang_viewpager);
        viewpagerall=(ViewPager)findViewById(R.id.fit_fengxiang_viewpagerall);

        mAdapter = new FittimeErjiFengxiangAdapter();
        mAdapterall = new FittimeErjiFengxiangAdapterall();
//        for(int i=0;i<5;i++) {
//            View v1 = LayoutInflater.from(FittimeFengxiangActivity.this).inflate(R.layout.fittime_fengxiang_relayout, null);
//            viewContainter.add(v1);
//        }
        fenxiang_jiexi_all();
        fenxiang_jiexi_geren();
        fit_fengxiang_geren.requestFocus();
        viewpager.setAdapter(mAdapter);
        viewpagerall.setAdapter(mAdapterall);
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    fittime_fengxian_jiantou1.setVisibility(View.GONE);
                } else {
                    fittime_fengxian_jiantou1.setVisibility(View.VISIBLE);
                }
                if (position == viewContainter.size() - 1) {
                    fittime_fengxian_jiantou2.setVisibility(View.GONE);
                } else {
                    fittime_fengxian_jiantou2.setVisibility(View.VISIBLE);
                }
                fittime_fengxiang_yema.setText((position + 1) + "/" + viewContainter.size());
                if(position==viewpager.getCurrentItem()) {
                    if (YEMA_NUM < position) {
                        View a = viewContainter.get(position);
                        FittimeFengxiangDemo b=(FittimeFengxiangDemo)a.findViewById(R.id.fittime_fengxian_demo1);
                        ImageView iv= (ImageView)b.findViewById(R.id.fit_fengxiang_demo_gb);
                        iv.requestFocus();
                        YEMA_NUM = position;
                    }
                    if (YEMA_NUM > position) {
                        View a = viewContainter.get(position);
                        FittimeFengxiangDemo b=(FittimeFengxiangDemo)a.findViewById(R.id.fittime_fengxian_demo3);
                        ImageView iv= (ImageView)b.findViewById(R.id.fit_fengxiang_demo_gb);
                        iv.requestFocus();
                        YEMA_NUM = position;
                    }
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewpagerall.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    fittime_fengxian_jiantou1.setVisibility(View.GONE);
                } else {
                    fittime_fengxian_jiantou1.setVisibility(View.VISIBLE);
                }
                if (position == viewContainterall.size() - 1) {
                    fittime_fengxian_jiantou2.setVisibility(View.GONE);
                } else {
                    fittime_fengxian_jiantou2.setVisibility(View.VISIBLE);
                }
                fittime_fengxiang_yema.setText((position + 1) + "/" + viewContainterall.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        fittime_fengxiang_yema.setText("1/" + viewContainter.size());

    }
    public void fenxiang_jiexi_geren(){
        YEMA_VIEW=0;
        viewpager.setCurrentItem(0);
        RequestQueue rq = Volley.newRequestQueue(this.getApplicationContext());
        JsonObjectRequest request = new JsonObjectRequest("http://61.191.46.214:8087/fittime//shared/user?userid="+ Util.getDeviceId(FittimeFengxiangActivity.this), null,
//        JsonObjectRequest request = new JsonObjectRequest("http://61.191.46.214:8087/fittime//shared/all", null,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        try {
                            JSONObject obj=jsonObject.getJSONObject("data");
                            JSONArray arr=obj.getJSONArray("list");
                            int a=arr.length()/6;
                            if(arr.length()%6!=0){
                                a+=1;
                            }
                            for(int i=0;i<a;i++) {
                                if(a==1){

                                        fittime_fengxian_jiantou1.setVisibility(View.GONE);
                                        fittime_fengxian_jiantou2.setVisibility(View.GONE);

                                }
                                View v1 = LayoutInflater.from(FittimeFengxiangActivity.this).inflate(R.layout.fittime_fengxiang_relayout, null);
                                viewContainter.add(v1);
                                FittimeFengxiangDemo d1=(FittimeFengxiangDemo)v1.findViewById(R.id.fittime_fengxian_demo1);
                                FittimeFengxiangDemo d2=(FittimeFengxiangDemo)v1.findViewById(R.id.fittime_fengxian_demo2);
                                FittimeFengxiangDemo d3=(FittimeFengxiangDemo)v1.findViewById(R.id.fittime_fengxian_demo3);
                                FittimeFengxiangDemo d4=(FittimeFengxiangDemo)v1.findViewById(R.id.fittime_fengxian_demo4);
                                FittimeFengxiangDemo d5=(FittimeFengxiangDemo)v1.findViewById(R.id.fittime_fengxian_demo5);
                                FittimeFengxiangDemo d6=(FittimeFengxiangDemo)v1.findViewById(R.id.fittime_fengxian_demo6);
                                if(YEMA_VIEW<arr.length()){
                                    JSONObject jo=arr.getJSONObject(YEMA_VIEW);
                                    final CircleImageView iv=(CircleImageView)d1.findViewById(R.id.fittime_fengxian_touxiang);
                                    ImageView iv2=(ImageView)d1.findViewById(R.id.fittime_fenxiang_ditu);
                                    TextView tv1=(TextView)d1.findViewById(R.id.fittime_fengxian_tv1);
                                    TextView tv2=(TextView)d1.findViewById(R.id.fittime_fengxian_tv2);
                                    TextView tv3=(TextView)d1.findViewById(R.id.fittime_fengxian_tv3);
                                    TextView tv4=(TextView)d1.findViewById(R.id.fittime_fengxian_tv4);
                                    TextView tv5=(TextView)d1.findViewById(R.id.fittime_fengxian_tv5);
                                    TextView tv6=(TextView)d1.findViewById(R.id.fittime_fengxian_tv6);
                                    tv1.setText(jo.getString("wxNickname"));
                                    tv2.setText(jo.getString("datetime"));
                                    tv3.setText(jo.getString("title"));
                                    tv4.setText("");
                                    int s=jo.getInt("duration");
                                    int min=s/60;
                                    int col=s*335/3600;
                                    tv5.setText(min+" 分钟");
                                    tv6.setText(col + " 卡路里");
                                    String pindao=jo.getString("category");
                                    dituchang(iv2, pindao);
                                    ImageView iv3=(ImageView)d1.findViewById(R.id.fittime_fengxiang_biaoqing);
                                    int biaoqing=jo.getInt("emoji");
                                    getbiaoqing(iv3, biaoqing);
                                    String iv_url=jo.getString("wxPortrait");
                                    RequestQueue mQueue = Volley.newRequestQueue(FittimeFengxiangActivity.this);
                                    ImageRequest imageRequest = new ImageRequest(
                                            iv_url,
                                            new Response.Listener<Bitmap>() {
                                                @Override
                                                public void onResponse(Bitmap response) {
                                                    iv.setImageBitmap(response);
                                                    map.put(YEMA_VIEW + "num1", response);
                                                }
                                            }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
                                        @Override
                                        public void onErrorResponse(VolleyError error) {
//                                    erweima_login.setImageResource(R.drawable.default_image);
                                        }
                                    });
                                    mQueue.add(imageRequest);
                                }else{
                                    d1.setVisibility(View.GONE);
                                }
                                if(YEMA_VIEW+1<arr.length()){
                                    JSONObject jo=arr.getJSONObject(YEMA_VIEW+1);
                                    final CircleImageView iv=(CircleImageView)d2.findViewById(R.id.fittime_fengxian_touxiang);
                                    ImageView iv2=(ImageView)d2.findViewById(R.id.fittime_fenxiang_ditu);
                                    TextView tv1=(TextView)d2.findViewById(R.id.fittime_fengxian_tv1);
                                    TextView tv2=(TextView)d2.findViewById(R.id.fittime_fengxian_tv2);
                                    TextView tv3=(TextView)d2.findViewById(R.id.fittime_fengxian_tv3);
                                    TextView tv4=(TextView)d2.findViewById(R.id.fittime_fengxian_tv4);
                                    TextView tv5=(TextView)d2.findViewById(R.id.fittime_fengxian_tv5);
                                    TextView tv6=(TextView)d2.findViewById(R.id.fittime_fengxian_tv6);
                                    tv1.setText(jo.getString("wxNickname"));
                                    tv2.setText(jo.getString("datetime"));
                                    tv3.setText(jo.getString("title"));
                                    tv4.setText("");
                                    int s=jo.getInt("duration");
                                    int min=s/60;
                                    int col=s*335/3600;
                                    tv5.setText(min+" 分钟");
                                    tv6.setText(col + " 卡路里");
                                    String pindao=jo.getString("category");
                                    dituchang(iv2, pindao);
                                    ImageView iv3=(ImageView)d2.findViewById(R.id.fittime_fengxiang_biaoqing);
                                    int biaoqing=jo.getInt("emoji");
                                    getbiaoqing(iv3, biaoqing);
                                    String iv_url=jo.getString("wxPortrait");
                                    RequestQueue mQueue = Volley.newRequestQueue(FittimeFengxiangActivity.this);
                                    ImageRequest imageRequest = new ImageRequest(
                                            iv_url,
                                            new Response.Listener<Bitmap>() {
                                                @Override
                                                public void onResponse(Bitmap response) {
                                                    iv.setImageBitmap(response);
                                                    map.put(YEMA_VIEW + "num2", response);
                                                }
                                            }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
                                        @Override
                                        public void onErrorResponse(VolleyError error) {
//                                    erweima_login.setImageResource(R.drawable.default_image);
                                        }
                                    });
                                    mQueue.add(imageRequest);
                                }else{
                                    d2.setVisibility(View.INVISIBLE);
                                }
                                if(YEMA_VIEW+2<arr.length()){
                                    JSONObject jo=arr.getJSONObject(YEMA_VIEW+2);
                                    final CircleImageView iv=(CircleImageView)d3.findViewById(R.id.fittime_fengxian_touxiang);
                                    ImageView iv2=(ImageView)d3.findViewById(R.id.fittime_fenxiang_ditu);
                                    TextView tv1=(TextView)d3.findViewById(R.id.fittime_fengxian_tv1);
                                    TextView tv2=(TextView)d3.findViewById(R.id.fittime_fengxian_tv2);
                                    TextView tv3=(TextView)d3.findViewById(R.id.fittime_fengxian_tv3);
                                    TextView tv4=(TextView)d3.findViewById(R.id.fittime_fengxian_tv4);
                                    TextView tv5=(TextView)d3.findViewById(R.id.fittime_fengxian_tv5);
                                    TextView tv6=(TextView)d3.findViewById(R.id.fittime_fengxian_tv6);
                                    tv1.setText(jo.getString("wxNickname"));
                                    tv2.setText(jo.getString("datetime"));
                                    tv3.setText(jo.getString("title"));
                                    tv4.setText("");
                                    int s=jo.getInt("duration");
                                    int min=s/60;
                                    int col=s*335/3600;
                                    tv5.setText(min+" 分钟");
                                    tv6.setText(col + " 卡路里");
                                    String pindao=jo.getString("category");
                                    dituchang(iv2, pindao);
                                    ImageView iv3=(ImageView)d3.findViewById(R.id.fittime_fengxiang_biaoqing);
                                    int biaoqing=jo.getInt("emoji");
                                    getbiaoqing(iv3, biaoqing);
                                    String iv_url=jo.getString("wxPortrait");
                                    RequestQueue mQueue = Volley.newRequestQueue(FittimeFengxiangActivity.this);
                                    ImageRequest imageRequest = new ImageRequest(
                                            iv_url,
                                            new Response.Listener<Bitmap>() {
                                                @Override
                                                public void onResponse(Bitmap response) {
                                                    iv.setImageBitmap(response);
                                                    map.put(YEMA_VIEW + "num3", response);
                                                }
                                            }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
                                        @Override
                                        public void onErrorResponse(VolleyError error) {
//                                    erweima_login.setImageResource(R.drawable.default_image);
                                        }
                                    });
                                    mQueue.add(imageRequest);
                                }else{
                                    d3.setVisibility(View.GONE);
                                }
                                if(YEMA_VIEW+3<arr.length()){
                                    JSONObject jo=arr.getJSONObject(YEMA_VIEW+3);
                                    final CircleImageView iv=(CircleImageView)d4.findViewById(R.id.fittime_fengxian_touxiang);
                                    ImageView iv2=(ImageView)d4.findViewById(R.id.fittime_fenxiang_ditu);
                                    TextView tv1=(TextView)d4.findViewById(R.id.fittime_fengxian_tv1);
                                    TextView tv2=(TextView)d4.findViewById(R.id.fittime_fengxian_tv2);
                                    TextView tv3=(TextView)d4.findViewById(R.id.fittime_fengxian_tv3);
                                    TextView tv4=(TextView)d4.findViewById(R.id.fittime_fengxian_tv4);
                                    TextView tv5=(TextView)d4.findViewById(R.id.fittime_fengxian_tv5);
                                    TextView tv6=(TextView)d4.findViewById(R.id.fittime_fengxian_tv6);
                                    tv1.setText(jo.getString("wxNickname"));
                                    tv2.setText(jo.getString("datetime"));
                                    tv3.setText(jo.getString("title"));
                                    tv4.setText("");
                                    int s=jo.getInt("duration");
                                    int min=s/60;
                                    int col=s*335/3600;
                                    tv5.setText(min+" 分钟");
                                    tv6.setText(col + " 卡路里");
                                    String pindao=jo.getString("category");
                                    dituchang(iv2, pindao);
                                    ImageView iv3=(ImageView)d4.findViewById(R.id.fittime_fengxiang_biaoqing);
                                    int biaoqing=jo.getInt("emoji");
                                    getbiaoqing(iv3, biaoqing);
                                    String iv_url=jo.getString("wxPortrait");
                                    RequestQueue mQueue = Volley.newRequestQueue(FittimeFengxiangActivity.this);
                                    ImageRequest imageRequest = new ImageRequest(
                                            iv_url,
                                            new Response.Listener<Bitmap>() {
                                                @Override
                                                public void onResponse(Bitmap response) {
                                                    iv.setImageBitmap(response);
                                                    map.put(YEMA_VIEW + "num4", response);
                                                }
                                            }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
                                        @Override
                                        public void onErrorResponse(VolleyError error) {
//                                    erweima_login.setImageResource(R.drawable.default_image);
                                        }
                                    });
                                    mQueue.add(imageRequest);
                                }else{
                                    d4.setVisibility(View.GONE);
                                }
                                if(YEMA_VIEW+4<arr.length()){
                                    JSONObject jo=arr.getJSONObject(YEMA_VIEW+4);
                                    final CircleImageView iv=(CircleImageView)d5.findViewById(R.id.fittime_fengxian_touxiang);
                                    ImageView iv2=(ImageView)d5.findViewById(R.id.fittime_fenxiang_ditu);
                                    TextView tv1=(TextView)d5.findViewById(R.id.fittime_fengxian_tv1);
                                    TextView tv2=(TextView)d5.findViewById(R.id.fittime_fengxian_tv2);
                                    TextView tv3=(TextView)d5.findViewById(R.id.fittime_fengxian_tv3);
                                    TextView tv4=(TextView)d5.findViewById(R.id.fittime_fengxian_tv4);
                                    TextView tv5=(TextView)d5.findViewById(R.id.fittime_fengxian_tv5);
                                    TextView tv6=(TextView)d5.findViewById(R.id.fittime_fengxian_tv6);
                                    tv1.setText(jo.getString("wxNickname"));
                                    tv2.setText(jo.getString("datetime"));
                                    tv3.setText(jo.getString("title"));
                                    tv4.setText("");
                                    int s=jo.getInt("duration");
                                    int min=s/60;
                                    int col=s*335/3600;
                                    tv5.setText(min+" 分钟");
                                    tv6.setText(col + " 卡路里");
                                    String pindao=jo.getString("category");
                                    dituchang(iv2, pindao);
                                    ImageView iv3=(ImageView)d5.findViewById(R.id.fittime_fengxiang_biaoqing);
                                    int biaoqing=jo.getInt("emoji");
                                    getbiaoqing(iv3, biaoqing);
                                    String iv_url=jo.getString("wxPortrait");
                                    RequestQueue mQueue = Volley.newRequestQueue(FittimeFengxiangActivity.this);
                                    ImageRequest imageRequest = new ImageRequest(
                                            iv_url,
                                            new Response.Listener<Bitmap>() {
                                                @Override
                                                public void onResponse(Bitmap response) {
                                                    iv.setImageBitmap(response);
                                                    map.put(YEMA_VIEW + "num5", response);
                                                }
                                            }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
                                        @Override
                                        public void onErrorResponse(VolleyError error) {
//                                    erweima_login.setImageResource(R.drawable.default_image);
                                        }
                                    });
                                    mQueue.add(imageRequest);
                                }else{
                                    d5.setVisibility(View.GONE);
                                }
                                if(YEMA_VIEW+5<arr.length()){
                                    JSONObject jo=arr.getJSONObject(YEMA_VIEW+5);
                                    final CircleImageView iv=(CircleImageView)d6.findViewById(R.id.fittime_fengxian_touxiang);
                                    ImageView iv2=(ImageView)d6.findViewById(R.id.fittime_fenxiang_ditu);
                                    TextView tv1=(TextView)d6.findViewById(R.id.fittime_fengxian_tv1);
                                    TextView tv2=(TextView)d6.findViewById(R.id.fittime_fengxian_tv2);
                                    TextView tv3=(TextView)d6.findViewById(R.id.fittime_fengxian_tv3);
                                    TextView tv4=(TextView)d6.findViewById(R.id.fittime_fengxian_tv4);
                                    TextView tv5=(TextView)d6.findViewById(R.id.fittime_fengxian_tv5);
                                    TextView tv6=(TextView)d6.findViewById(R.id.fittime_fengxian_tv6);
                                    tv1.setText(jo.getString("wxNickname"));
                                    tv2.setText(jo.getString("datetime"));
                                    tv3.setText(jo.getString("title"));
                                    tv4.setText("");
                                    int s=jo.getInt("duration");
                                    int min=s/60;
                                    int col=s*335/3600;
                                    tv5.setText(min+" 分钟");
                                    tv6.setText(col + " 卡路里");
                                    String pindao=jo.getString("category");
                                    dituchang(iv2, pindao);
                                    ImageView iv3=(ImageView)d6.findViewById(R.id.fittime_fengxiang_biaoqing);
                                    int biaoqing=jo.getInt("emoji");
                                    getbiaoqing(iv3, biaoqing);
                                    String iv_url=jo.getString("wxPortrait");
                                    RequestQueue mQueue = Volley.newRequestQueue(FittimeFengxiangActivity.this);
                                    ImageRequest imageRequest = new ImageRequest(
                                            iv_url,
                                            new Response.Listener<Bitmap>() {
                                                @Override
                                                public void onResponse(Bitmap response) {
                                                    iv.setImageBitmap(response);
                                                    map.put(YEMA_VIEW + "num6", response);
                                                }
                                            }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
                                        @Override
                                        public void onErrorResponse(VolleyError error) {
//                                    erweima_login.setImageResource(R.drawable.default_image);
                                        }
                                    });
                                    mQueue.add(imageRequest);
                                }else{
                                    d6.setVisibility(View.GONE);
                                }

                                YEMA_VIEW+=6;
                            }
                            fittime_fengxiang_yema.setText("1/"+viewContainter.size());
                            mAdapter.notifyDataSetChanged();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
            }
        }

        );
        rq.add(request);

    }
    public void fenxiang_jiexi_all(){
        YEMA_VIEWALL=0;
        viewpagerall.setCurrentItem(0);
        RequestQueue rq = Volley.newRequestQueue(this.getApplicationContext());
//        JsonObjectRequest request = new JsonObjectRequest("http://61.191.46.214:8087/fittime//shared/user?userid="+ Util.getDeviceId(FittimeFengxiangActivity.this), null,
        JsonObjectRequest request = new JsonObjectRequest("http://61.191.46.214:8087/fittime//shared/all", null,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        try {
                            JSONObject obj=jsonObject.getJSONObject("data");
                            JSONArray arr=obj.getJSONArray("list");
                            int a=arr.length()/6;
                            if(arr.length()%6!=0){
                                a+=1;
                            }
                            for(int i=0;i<a;i++) {
                                View v1 = LayoutInflater.from(FittimeFengxiangActivity.this).inflate(R.layout.fittime_fengxiang_relayoutall, null);
                                viewContainterall.add(v1);
                                FittimeFengxiangDemo d1=(FittimeFengxiangDemo)v1.findViewById(R.id.fittime_fengxian_demo11);
                                FittimeFengxiangDemo d2=(FittimeFengxiangDemo)v1.findViewById(R.id.fittime_fengxian_demo12);
                                FittimeFengxiangDemo d3=(FittimeFengxiangDemo)v1.findViewById(R.id.fittime_fengxian_demo13);
                                FittimeFengxiangDemo d4=(FittimeFengxiangDemo)v1.findViewById(R.id.fittime_fengxian_demo14);
                                FittimeFengxiangDemo d5=(FittimeFengxiangDemo)v1.findViewById(R.id.fittime_fengxian_demo15);
                                FittimeFengxiangDemo d6=(FittimeFengxiangDemo)v1.findViewById(R.id.fittime_fengxian_demo16);
                                if(YEMA_VIEWALL<arr.length()){
                                    JSONObject jo=arr.getJSONObject(YEMA_VIEWALL);
                                    final CircleImageView iv=(CircleImageView)d1.findViewById(R.id.fittime_fengxian_touxiang);
                                    ImageView iv2=(ImageView)d1.findViewById(R.id.fittime_fenxiang_ditu);
                                    TextView tv1=(TextView)d1.findViewById(R.id.fittime_fengxian_tv1);
                                    TextView tv2=(TextView)d1.findViewById(R.id.fittime_fengxian_tv2);
                                    TextView tv3=(TextView)d1.findViewById(R.id.fittime_fengxian_tv3);
                                    TextView tv4=(TextView)d1.findViewById(R.id.fittime_fengxian_tv4);
                                    TextView tv5=(TextView)d1.findViewById(R.id.fittime_fengxian_tv5);
                                    TextView tv6=(TextView)d1.findViewById(R.id.fittime_fengxian_tv6);
                                    tv1.setText(jo.getString("wxNickname"));
                                    tv2.setText(jo.getString("datetime"));
                                    tv3.setText(jo.getString("title"));
                                    tv4.setText("");
                                    int s=jo.getInt("duration");
                                    int min=s/60;
                                    int col=s*335/3600;
                                    tv5.setText(min+" 分钟");
                                    tv6.setText(col + " 卡路里");
                                    String pindao=jo.getString("category");
                                    dituchang(iv2, pindao);
                                    ImageView iv3=(ImageView)d1.findViewById(R.id.fittime_fengxiang_biaoqing);
                                    int biaoqing=jo.getInt("emoji");
                                    getbiaoqing(iv3, biaoqing);
                                    String iv_url=jo.getString("wxPortrait");
                                    RequestQueue mQueue = Volley.newRequestQueue(FittimeFengxiangActivity.this);
                                    ImageRequest imageRequest = new ImageRequest(
                                            iv_url,
                                            new Response.Listener<Bitmap>() {
                                                @Override
                                                public void onResponse(Bitmap response) {
                                                    iv.setImageBitmap(response);
                                                    map.put(YEMA_VIEWALL+"num11",response);
                                                }
                                            }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
                                        @Override
                                        public void onErrorResponse(VolleyError error) {
//                                    erweima_login.setImageResource(R.drawable.default_image);
                                        }
                                    });
                                    mQueue.add(imageRequest);
                                }else{
                                    d1.setVisibility(View.GONE);
                                }
                                if(YEMA_VIEWALL+1<arr.length()){
                                    JSONObject jo=arr.getJSONObject(YEMA_VIEWALL+1);
                                    final CircleImageView iv=(CircleImageView)d2.findViewById(R.id.fittime_fengxian_touxiang);
                                    ImageView iv2=(ImageView)d2.findViewById(R.id.fittime_fenxiang_ditu);
                                    TextView tv1=(TextView)d2.findViewById(R.id.fittime_fengxian_tv1);
                                    TextView tv2=(TextView)d2.findViewById(R.id.fittime_fengxian_tv2);
                                    TextView tv3=(TextView)d2.findViewById(R.id.fittime_fengxian_tv3);
                                    TextView tv4=(TextView)d2.findViewById(R.id.fittime_fengxian_tv4);
                                    TextView tv5=(TextView)d2.findViewById(R.id.fittime_fengxian_tv5);
                                    TextView tv6=(TextView)d2.findViewById(R.id.fittime_fengxian_tv6);
                                    tv1.setText(jo.getString("wxNickname"));
                                    tv2.setText(jo.getString("datetime"));
                                    tv3.setText(jo.getString("title"));
                                    tv4.setText("");
                                    int s=jo.getInt("duration");
                                    int min=s/60;
                                    int col=s*335/3600;
                                    tv5.setText(min+" 分钟");
                                    tv6.setText(col + " 卡路里");
                                    String pindao=jo.getString("category");
                                    dituchang(iv2, pindao);
                                    ImageView iv3=(ImageView)d2.findViewById(R.id.fittime_fengxiang_biaoqing);
                                    int biaoqing=jo.getInt("emoji");
                                    getbiaoqing(iv3, biaoqing);
                                    String iv_url=jo.getString("wxPortrait");
                                    RequestQueue mQueue = Volley.newRequestQueue(FittimeFengxiangActivity.this);
                                    ImageRequest imageRequest = new ImageRequest(
                                            iv_url,
                                            new Response.Listener<Bitmap>() {
                                                @Override
                                                public void onResponse(Bitmap response) {
                                                    iv.setImageBitmap(response);
                                                    map.put(YEMA_VIEWALL + "num12", response);
                                                }
                                            }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
                                        @Override
                                        public void onErrorResponse(VolleyError error) {
//                                    erweima_login.setImageResource(R.drawable.default_image);
                                        }
                                    });
                                    mQueue.add(imageRequest);
                                }else{
                                    d2.setVisibility(View.INVISIBLE);
                                }
                                if(YEMA_VIEWALL+2<arr.length()){
                                    JSONObject jo=arr.getJSONObject(YEMA_VIEWALL+2);
                                    final CircleImageView iv=(CircleImageView)d3.findViewById(R.id.fittime_fengxian_touxiang);
                                    ImageView iv2=(ImageView)d3.findViewById(R.id.fittime_fenxiang_ditu);
                                    TextView tv1=(TextView)d3.findViewById(R.id.fittime_fengxian_tv1);
                                    TextView tv2=(TextView)d3.findViewById(R.id.fittime_fengxian_tv2);
                                    TextView tv3=(TextView)d3.findViewById(R.id.fittime_fengxian_tv3);
                                    TextView tv4=(TextView)d3.findViewById(R.id.fittime_fengxian_tv4);
                                    TextView tv5=(TextView)d3.findViewById(R.id.fittime_fengxian_tv5);
                                    TextView tv6=(TextView)d3.findViewById(R.id.fittime_fengxian_tv6);
                                    tv1.setText(jo.getString("wxNickname"));
                                    tv2.setText(jo.getString("datetime"));
                                    tv3.setText(jo.getString("title"));
                                    tv4.setText("");
                                    int s=jo.getInt("duration");
                                    int min=s/60;
                                    int col=s*335/3600;
                                    tv5.setText(min+" 分钟");
                                    tv6.setText(col + " 卡路里");
                                    String pindao=jo.getString("category");
                                    dituchang(iv2, pindao);
                                    ImageView iv3=(ImageView)d3.findViewById(R.id.fittime_fengxiang_biaoqing);
                                    int biaoqing=jo.getInt("emoji");
                                    getbiaoqing(iv3, biaoqing);
                                    String iv_url=jo.getString("wxPortrait");
                                    RequestQueue mQueue = Volley.newRequestQueue(FittimeFengxiangActivity.this);
                                    ImageRequest imageRequest = new ImageRequest(
                                            iv_url,
                                            new Response.Listener<Bitmap>() {
                                                @Override
                                                public void onResponse(Bitmap response) {
                                                    iv.setImageBitmap(response);
                                                    map.put(YEMA_VIEWALL + "num13", response);
                                                }
                                            }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
                                        @Override
                                        public void onErrorResponse(VolleyError error) {
//                                    erweima_login.setImageResource(R.drawable.default_image);
                                        }
                                    });
                                    mQueue.add(imageRequest);
                                }else{
                                    d3.setVisibility(View.GONE);
                                }
                                if(YEMA_VIEWALL+3<arr.length()){
                                    JSONObject jo=arr.getJSONObject(YEMA_VIEWALL+3);
                                    final CircleImageView iv=(CircleImageView)d4.findViewById(R.id.fittime_fengxian_touxiang);
                                    ImageView iv2=(ImageView)d4.findViewById(R.id.fittime_fenxiang_ditu);
                                    TextView tv1=(TextView)d4.findViewById(R.id.fittime_fengxian_tv1);
                                    TextView tv2=(TextView)d4.findViewById(R.id.fittime_fengxian_tv2);
                                    TextView tv3=(TextView)d4.findViewById(R.id.fittime_fengxian_tv3);
                                    TextView tv4=(TextView)d4.findViewById(R.id.fittime_fengxian_tv4);
                                    TextView tv5=(TextView)d4.findViewById(R.id.fittime_fengxian_tv5);
                                    TextView tv6=(TextView)d4.findViewById(R.id.fittime_fengxian_tv6);
                                    tv1.setText(jo.getString("wxNickname"));
                                    tv2.setText(jo.getString("datetime"));
                                    tv3.setText(jo.getString("title"));
                                    tv4.setText("");
                                    int s=jo.getInt("duration");
                                    int min=s/60;
                                    int col=s*335/3600;
                                    tv5.setText(min+" 分钟");
                                    tv6.setText(col + " 卡路里");
                                    String pindao=jo.getString("category");
                                    dituchang(iv2, pindao);
                                    ImageView iv3=(ImageView)d4.findViewById(R.id.fittime_fengxiang_biaoqing);
                                    int biaoqing=jo.getInt("emoji");
                                    getbiaoqing(iv3, biaoqing);
                                    String iv_url=jo.getString("wxPortrait");
                                    RequestQueue mQueue = Volley.newRequestQueue(FittimeFengxiangActivity.this);
                                    ImageRequest imageRequest = new ImageRequest(
                                            iv_url,
                                            new Response.Listener<Bitmap>() {
                                                @Override
                                                public void onResponse(Bitmap response) {
                                                    iv.setImageBitmap(response);
                                                    map.put(YEMA_VIEWALL + "num14", response);
                                                }
                                            }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
                                        @Override
                                        public void onErrorResponse(VolleyError error) {
//                                    erweima_login.setImageResource(R.drawable.default_image);
                                        }
                                    });
                                    mQueue.add(imageRequest);
                                }else{
                                    d4.setVisibility(View.GONE);
                                }
                                if(YEMA_VIEWALL+4<arr.length()){
                                    JSONObject jo=arr.getJSONObject(YEMA_VIEWALL+4);
                                    final CircleImageView iv=(CircleImageView)d5.findViewById(R.id.fittime_fengxian_touxiang);
                                    ImageView iv2=(ImageView)d5.findViewById(R.id.fittime_fenxiang_ditu);
                                    TextView tv1=(TextView)d5.findViewById(R.id.fittime_fengxian_tv1);
                                    TextView tv2=(TextView)d5.findViewById(R.id.fittime_fengxian_tv2);
                                    TextView tv3=(TextView)d5.findViewById(R.id.fittime_fengxian_tv3);
                                    TextView tv4=(TextView)d5.findViewById(R.id.fittime_fengxian_tv4);
                                    TextView tv5=(TextView)d5.findViewById(R.id.fittime_fengxian_tv5);
                                    TextView tv6=(TextView)d5.findViewById(R.id.fittime_fengxian_tv6);
                                    tv1.setText(jo.getString("wxNickname"));
                                    tv2.setText(jo.getString("datetime"));
                                    tv3.setText(jo.getString("title"));
                                    tv4.setText("");
                                    int s=jo.getInt("duration");
                                    int min=s/60;
                                    int col=s*335/3600;
                                    tv5.setText(min+" 分钟");
                                    tv6.setText(col + " 卡路里");
                                    String pindao=jo.getString("category");
                                    dituchang(iv2, pindao);
                                    ImageView iv3=(ImageView)d5.findViewById(R.id.fittime_fengxiang_biaoqing);
                                    int biaoqing=jo.getInt("emoji");
                                    getbiaoqing(iv3, biaoqing);
                                    String iv_url=jo.getString("wxPortrait");
                                    RequestQueue mQueue = Volley.newRequestQueue(FittimeFengxiangActivity.this);
                                    ImageRequest imageRequest = new ImageRequest(
                                            iv_url,
                                            new Response.Listener<Bitmap>() {
                                                @Override
                                                public void onResponse(Bitmap response) {
                                                    iv.setImageBitmap(response);
                                                    map.put(YEMA_VIEWALL + "num15", response);
                                                }
                                            }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
                                        @Override
                                        public void onErrorResponse(VolleyError error) {
//                                    erweima_login.setImageResource(R.drawable.default_image);
                                        }
                                    });
                                    mQueue.add(imageRequest);
                                }else{
                                    d5.setVisibility(View.GONE);
                                }
                                if(YEMA_VIEWALL+5<arr.length()){
                                    JSONObject jo=arr.getJSONObject(YEMA_VIEWALL+5);
                                    final CircleImageView iv=(CircleImageView)d6.findViewById(R.id.fittime_fengxian_touxiang);
                                    ImageView iv2=(ImageView)d6.findViewById(R.id.fittime_fenxiang_ditu);
                                    TextView tv1=(TextView)d6.findViewById(R.id.fittime_fengxian_tv1);
                                    TextView tv2=(TextView)d6.findViewById(R.id.fittime_fengxian_tv2);
                                    TextView tv3=(TextView)d6.findViewById(R.id.fittime_fengxian_tv3);
                                    TextView tv4=(TextView)d6.findViewById(R.id.fittime_fengxian_tv4);
                                    TextView tv5=(TextView)d6.findViewById(R.id.fittime_fengxian_tv5);
                                    TextView tv6=(TextView)d6.findViewById(R.id.fittime_fengxian_tv6);
                                    tv1.setText(jo.getString("wxNickname"));
                                    tv2.setText(jo.getString("datetime"));
                                    tv3.setText(jo.getString("title"));
                                    tv4.setText("");
                                    int s=jo.getInt("duration");
                                    int min=s/60;
                                    int col=s*335/3600;
                                    tv5.setText(min+" 分钟");
                                    tv6.setText(col + " 卡路里");
                                    String pindao=jo.getString("category");
                                    dituchang(iv2, pindao);
                                    ImageView iv3=(ImageView)d6.findViewById(R.id.fittime_fengxiang_biaoqing);
                                    int biaoqing=jo.getInt("emoji");
                                    getbiaoqing(iv3, biaoqing);
                                    String iv_url=jo.getString("wxPortrait");
                                    RequestQueue mQueue = Volley.newRequestQueue(FittimeFengxiangActivity.this);
                                    ImageRequest imageRequest = new ImageRequest(
                                            iv_url,
                                            new Response.Listener<Bitmap>() {
                                                @Override
                                                public void onResponse(Bitmap response) {
                                                    iv.setImageBitmap(response);
                                                    map.put(YEMA_VIEWALL+"num16",response);
                                                }
                                            }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
                                        @Override
                                        public void onErrorResponse(VolleyError error) {
//                                    erweima_login.setImageResource(R.drawable.default_image);
                                        }
                                    });
                                    mQueue.add(imageRequest);
                                }else{
                                    d6.setVisibility(View.GONE);
                                }

                                YEMA_VIEWALL+=6;
                            }

                            mAdapterall.notifyDataSetChanged();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
            }
        }

        );
        rq.add(request);
        if(viewContainterall.size()==1){
            fittime_fengxian_jiantou1.setVisibility(View.GONE);
            fittime_fengxian_jiantou2.setVisibility(View.GONE);
        }
    }
    public class TimeThread extends Thread {
        @Override
        public void run () {
            do {
                try {
                    Thread.sleep(1000);
                    Message msg = new Message();
                    msg.what = msgKey1;
                    mHandler.sendMessage(msg);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } while(true);
        }
    }
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage (Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case msgKey1:
                    long sysTime = System.currentTimeMillis();
                    CharSequence sysTimeStr = DateFormat.format("hh:mm aa", sysTime);
                    fit_main_fengxiang_time.setText(sysTimeStr);
                    break;

                default:
                    break;
            }
        }
    };
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_fittime_fengxiang, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    private class FittimeErjiFengxiangAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return viewContainter.size();
        }

        //滑动切换的时候销毁当前的组件
        @Override
        public void destroyItem(ViewGroup container, int position,
                                Object object) {
            ((ViewPager) container).removeView(viewContainter.get(position));
        }

        //每次滑动的时候生成的组件
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ((ViewPager) container).addView(viewContainter.get(position));
            return viewContainter.get(position);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }

//        @Override
//        public float getPageWidth(int position) {
//            float num = 0.93f;
//            return num;
//        }
    }
    private class FittimeErjiFengxiangAdapterall extends PagerAdapter {
        @Override
        public int getCount() {
            return viewContainterall.size();
        }

        //滑动切换的时候销毁当前的组件
        @Override
        public void destroyItem(ViewGroup container, int position,
                                Object object) {
            ((ViewPager) container).removeView(viewContainterall.get(position));
        }

        //每次滑动的时候生成的组件
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ((ViewPager) container).addView(viewContainterall.get(position));
            return viewContainterall.get(position);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }

//        @Override
//        public float getPageWidth(int position) {
//            float num = 0.93f;
//            return num;
//        }
    }
    public void dituchang(ImageView v,String s){
        if(s.equals("001")){
            v.setImageResource(R.drawable.fitt_demo1);
        }else if(s.equals("002")){
            v.setImageResource(R.drawable.fitt_demo2);
        }
        else if(s.equals("003")){
            v.setImageResource(R.drawable.fitt_demo3);
        }
        else if(s.equals("004")){
            v.setImageResource(R.drawable.fitt_demo4);
        }
    }
    public void getbiaoqing(ImageView v,int num){
        if(num==1){
            v.setImageResource(R.drawable.fit_daka_face1);
        }else if(num==2){
            v.setImageResource(R.drawable.fit_daka_face2);
        }
        else if(num==3){
            v.setImageResource(R.drawable.fit_daka_face3);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Iterator iter = map.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next(); Object key = entry.getKey();
            Object val = entry.getValue();
            Bitmap a=(Bitmap)val;
            a.recycle();
        }
        System.gc();
    }
}
